local function getupvaluetable(func, inname)
    local i = 1
    while true do
        local name, value = debug.getupvalue(func, i)
        if name == nil then
            return
        end
        
        if name == inname then
            return i,value
        end
        
        i=i+1
    end
end


local lua = _P.lua
local idx, old_func = getupvaluetable(lua.CMD.new_bind_lucky_draw,'new_bind_lucky_draw')
local _, skynet = getupvaluetable(old_func,'skynet')
local _, cjson = getupvaluetable(old_func,'cjson')
local _,make_player_act_key = getupvaluetable(old_func,'make_player_act_key')
local _,pay_ticket = getupvaluetable(old_func,'pay_ticket')
local _,random_card = getupvaluetable(old_func,'random_card')
local _,utils = getupvaluetable(old_func,'utils')


local function new_bind_lucky_draw(act_id,conn,key,uid,conf)
    local act_data = conn:get(key)
    if not act_data then
        return skynet.retpack(false)
    end
    act_data = cjson.decode(act_data)

    local player_key  = make_player_act_key(uid)
    local player_data = act_data.player_data_map[player_key]
    if not player_data then
        return skynet.retpack(false)
    end

    --免费次数不够时,检查金币
    local award_cost = conf.ac_content.award_cost or 1000
    local free_count = (player_data.free_count or 0) + (player_data.bind_count or 0)
    print("new_bind_lucky_draw_free_count====",free_count)
    if free_count <= 0 then
        local ok,curr_coins = R().basesvr({key=uid}):call('.msg_handler','get_coins',uid)
        if not ok or (curr_coins or 0) < award_cost then
            return skynet.retpack(false)
        end
    end

    --扣钱加进库存
    local pay_sucess,pay_coins,use_bind = pay_ticket(uid,award_cost,player_data)
    if not pay_sucess then
        return skynet.retpack(false)
    end
    act_data.store_coins = (act_data.store_coins or 0) + pay_coins

    --抽奖次数增加,检查是否触发绑定的玩家免费次数增加
    player_data.award_count = (player_data.award_count or 0) + 1
    if player_data.award_count % conf.ac_content.advert_condition == 0 then --达到推广目标
        local bind_key = make_player_act_key(player_data.bind_uid)
        local bind_player_data = act_data.player_data_map[bind_key]
        if bind_player_data then
            bind_player_data.free_count = (bind_player_data.free_count or 0) + 1
            bind_player_data.history_free_count = (bind_player_data.history_free_count or 0) + 1
        end
    end

    --随机一个牌,判断中奖,发放奖励
    local ok,pay_money = R().basesvr({key=uid}):call('.msg_handler','get_pay_money',uid)
    print("get_pay_money===========",pay_money)
    if not ok then
        errlog("get_pay_money======")
    end

    local card_data = random_card(act_data.store_coins,conf,use_bind,pay_money)
    player_data.score = (player_data.score or 0) + card_data.award_score
    act_data.store_coins = (act_data.store_coins or 0) - card_data.award_coins

    local ret_data = {
        card_data = card_data,
        pay_coins = pay_coins,
        player_data = utils.shallow_copy(player_data),
    }

    local data_str = cjson.encode(act_data)
    conn:set(key,data_str)

    skynet.retpack(ret_data)
end

debug.setupvalue(lua.CMD.new_bind_lucky_draw,idx,new_bind_lucky_draw)